package com.demo.chapter3lambda;

import java.util.function.Function;

/**函数组合
 * @author dell
 *
 */
public class FunctionComposite {

	public static void demo(){
		Function<Integer, Integer> f = x -> x + 1;
		Function<Integer, Integer> g = x -> x * 2;
		//先对输入应用第一个函数，再对结果应用第二个函数，数学上是g(f(x))
		Function<Integer, Integer> h = f.andThen(g);
		System.out.println(h.apply(3));
	}

	public static void demo2(){
		Function<Integer, Integer> f = x -> x + 1;
		Function<Integer, Integer> g = x -> x * 2;
		//数学上是f(g(x))
		Function<Integer, Integer> h = f.compose(g);
		System.out.println(h.apply(3));
	}

	public static void main(String[] args) {
		demo();//8=(3+1)*2
		demo2();//7=3*2+1
	}

}
